#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
import pymysql

# 添加项目根目录到Python路径
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)


# 直接定义DB连接函数
def DB():
    return pymysql.connect(
        host="rm-2zer2f33rws29ogb1do.mysql.rds.aliyuncs.com",
        port=3306,
        user="zxy",
        password="zxy@800901",
        database="ruoyi",
    )


def read_table_structures():
    """读取指定表的结构"""
    tables = [
        'dz_purchase_detail',
        'dz_real_sales_bottomtable',
        'dz_real_sales_detail',
        'dz_vendor_info',
        'dz_vendor_settlement_statistics'
    ]

    try:
        conn = DB()
        cursor = conn.cursor()

        for table_name in tables:
            print(f"\n=== {table_name} 表结构 ===")

            # 获取表结构
            cursor.execute(f'DESCRIBE {table_name}')
            columns = cursor.fetchall()

            print(f"字段名\t\t类型\t\t\t是否为空\t键\t默认值\t\t额外")
            print("-" * 80)

            for col in columns:
                field, type_, null, key, default, extra = col
                default_str = str(default) if default is not None else 'NULL'
                print(f"{field:<20} {type_:<20} {null:<10} {key:<5} {default_str:<15} {extra:<15}")

            # 获取建表语句
            print(f"\n=== {table_name} 建表语句 ===")
            cursor.execute(f'SHOW CREATE TABLE {table_name}')
            create_table = cursor.fetchone()
            if create_table:
                print(create_table[1])
            print("\n" + "=" * 100)

        cursor.close()
        conn.close()

    except Exception as e:
        print(f"读取表结构时出错: {e}")


if __name__ == "__main__":
    read_table_structures()
